package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"web_app/logic"
	"web_app/modules"
)

func CreatePostHandler(c *gin.Context) {
	//获取参数校验

	p := new(modules.Post)
	if err := c.ShouldBindJSON(p); err != nil {
		zap.L().Error("create post with invalid param")
		ResponseError(c, CodeInvalidParam)
		return
	}
	p.AuthorID, _ = GetCurrentUser(c)
	//创建帖子
	if err := logic.CreatePost(p); err != nil {
		zap.L().Error("logic.CreatePost 创建帖子失败", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}

	//返回响应
	ResponseSuccess(c, "帖子创建成功")
}

func GetPostDetailHandler(c *gin.Context) {

	idStr := c.Param("id")
	fmt.Println(idStr)
	i, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		ResponseError(c, CodeInvalidParam)
		return
	}
	fmt.Println(i)
	apiPostDetail := logic.GetPostDetail(i)

	if apiPostDetail == nil {
		zap.L().Error("查询出错")
		ResponseError(c, CodeInvalidID)
		return
	}

	ResponseSuccess(c, apiPostDetail)
}
